package interceptor.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author quan
 * @Description 写博客页面、编辑博客、删除博客、登录博客、注册博客页面
 * @Date 2023/7/26 17:15
 */
@Slf4j
@RequestMapping("/aop")
@RestController
public class UserController {
    //模拟写博客
    @RequestMapping("/writeBlog1")
    public String writeBlog(){
        log.info("write blog ...");
        return "write blog ...";
    }
    //编辑博客
    @RequestMapping("/editBlog1")
    public String editBlog(){
        log.info("edit blog ...");
        return "edit blog ...";
    }
    //删除博客
    @RequestMapping("/deleteBlog1")
    public String deleteBlog(){
        log.info("deleteBlog ...");
        return "deleteBlog ...";
    }


    //注册博客
    @RequestMapping("/regBlog1")
    public String regBlog(){
        log.info("regBlog ...");
        return "regBlog ...";
    }

    //登录 博客
    @RequestMapping("/loginBlog1")
    public boolean loginBlog1(HttpServletRequest request,String username,String password){
        log.info("login ing...");
        //判断username和password是否为空
        //为空
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return false;
        }
        //如果账号和密码不为admin，就返回false
        if(!"admin".equals(username) || !"admin".equals(password)){
            return false;
        }
        //不为空:传入true，如果没有就创建session
        HttpSession session = request.getSession(true);
        //设置session
        session.setAttribute("username",username);
        return true;
    }
}
